﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;

namespace ERPStore.Tasks.Extensions
{
	public static class HttpExtensions
	{
		public static void Authenticate(this HttpRequestMessage request)
		{
			var apiKeyHeader = request.Headers.FirstOrDefault(h => "apikey".Equals(h.Key, StringComparison.InvariantCultureIgnoreCase));
			if (apiKeyHeader.Value == null)
			{
				throw new System.Security.SecurityException("this service required an api key");
			}
			if (!apiKeyHeader.Value.First().Equals(ERPStore.GlobalConfiguration.Configuration.Settings.ApiToken))
			{
				throw new System.Security.SecurityException("this service is denied with this api key");
			}
		}
	}
}
